<?php

namespace App\Component;

use Nette\Application\UI\Control,
	Nette\Application\UI\Form;

class Comment extends Control {
  
  private $em;
  private $video;
  private $limit;
  private $offset;
  
  /**
   * 
   * @param Video $video
   * @param EntityManager $entityManager
   * @param int $perPage
   */
  public function init($video, $entityManager ,$perPage = 4){
    $this->video = $video;
    $this->em = $entityManager;
    $this->offset = 0;
    $this->limit = $perPage;
  }

  /**
   * Comment form
   * @return \Nette\Application\UI\Form
   */
	public function createComponentCommentForm() {
		$form = new Form();
		$form->addTextArea('text', 'Komentář:')
        ->addRule(Form::FILLED, 'Něco nám napište')
        ->addRule(Form::MAX_LENGTH, 'Komentář může mít maximálně 255 znaků', 255);
                
    $form->addSubmit('send', 'Poslat');
		$form->onSuccess[] = callback($this, 'submitCommentForm');

		return $form;
	}

  /**
   * Comment form submit
   * @param \Nette\Application\UI\Form $form
   */
	public function submitCommentForm(Form $form) {
    if($this->presenter->isAjax() && $this->presenter->getUser()->isLoggedIn()){
      
      if(!$form->isValid()){
        $form->addError('Něco nám napište');
        $this->invalidateControl('comment');
      }
      $vatues = $form->getValues();
      $content = $vatues['text'];
      $userId = $this->presenter->getUser()->getId();
      $published = new \DateTime();
      $user = $this->em->getDao(\App\Model\Dao\User::getClassName())->findOneBy(array('id' => $userId));
      
      $comment = new \App\Model\Dao\VideoComment;
      $comment->setAll($user, $this->video, $published, $content);
      $this->em->persist($comment);
      $this->em->flush();
      
      $form->setValues(array('text' => ''), TRUE);
      $this->invalidateControl('comment');
    }
	}
  
  /**
   * Change page 
   * @param int $offset
   */
  public function handlePage($offset){
    if($offset < 0){
      $offset = 0;
    }
    $this->offset = $offset;
    
    if($this->presenter->isAjax()){
      $this->invalidateControl('comment');
    }
  }

  /**
   * Render component
   */
	public function render() {
    $commentDao = $this->em->getDao(\App\Model\Dao\VideoComment::getClassName());
    $commentsCount = $commentDao->countBy(array('video' => $this->video));
    $comments = $commentDao->findBy(array('video' => $this->video), array('published' => 'DESC'), $this->limit, $this->offset);
    $pagesTotal = floor($commentsCount / $this->limit);
    $currentPage = floor($this->offset / $this->limit);
    
		$template = $this->template;
		$template->setFile(__DIR__ . '/comment.latte');
    $template->comments = $comments;
    $this->template->offset = $this->offset;
    $this->template->limit = $this->limit;
    $this->template->commentsCount = $commentsCount;
    $this->template->pagesTotal = $pagesTotal;
    $this->template->currentPage = $currentPage;
    
		$template->render();
	}

}
